﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QSTOOL.Codes;
using System.Collections;
using System.Configuration;

namespace QSTOOL.Forms
{
    public partial class FormMain : Form
    {
        private ArrayList allCommands { get; set; }
        public XMLTool xml { get; private set; }
        private int commandStatus { get; set; }

        public FormMain()
        {
            InitializeComponent();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            this.xml = new XMLTool();
            this.allCommands = xml.allcommands;
        }

        private void menuExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnStep1_Click(object sender, EventArgs e)
        {
            //this.tsLabel.Text += "1";
            Work work = new Work();
            work.commands = (List<Command>)this.allCommands[0];
            work.frmOwner = this;

        }

        private void menuAbout_Click(object sender, EventArgs e)
        {
            FormAbout frmAbout = new FormAbout();
            frmAbout.ShowDialog();
        }

        private void menuCommandMaintain_Click(object sender, EventArgs e)
        {
            FormCommandMaintain frmCM = new FormCommandMaintain();
            //frmCM.allCommands = this.allCommands;
            frmCM.xml = this.xml;
            frmCM.ShowDialog();
        }

        private void menuTest_Click(object sender, EventArgs e)
        {
           
            
        }

        private void btnStep2_Click(object sender, EventArgs e)
        {
            ListViewItem lvi = new ListViewItem();
            lvi.SubItems.Add("111111111111111111111111111");
            lvi.StateImageIndex = 1;
            this.lsvResult.Items.Add(lvi);
            this.lsvResult.Items[this.lsvResult.Items.Count - 1].EnsureVisible();
        }

        public void AppendCommandList(Command cmd)
        {
            if (lsvCommandList.Items.Count > 0)
            {
                lsvCommandList.Items[lsvCommandList.Items.Count - 1].StateImageIndex = this.commandStatus;
            }

            ListViewItem lvi = new ListViewItem();
            lvi.StateImageIndex = ImageIndex.DOING;
            lvi.SubItems.Add(cmd.command);
            lvi.SubItems.Add(cmd.detail);
            lvi.SubItems.Add(cmd.source);
            lvi.SubItems.Add(cmd.target);
            lvi.SubItems.Add(cmd.description);

            lsvCommandList.Items.Add(lvi);
        }

        public void AppendResult(Result result)
        {
            ListViewItem lvi = new ListViewItem();
            if (result.ReturnCode == 0)
            {
                lvi.StateImageIndex = ImageIndex.SUCCESS;
                this.commandStatus = ImageIndex.SUCCESS;
            }
            else 
            {
                switch (result.CommandType)
                {
                    case Command.DB2:
                        if (result.ReturnCode == 1 || result.ReturnCode == 2)
                        {
                            lvi.StateImageIndex = ImageIndex.WARNING;
                            this.commandStatus = ImageIndex.WARNING;
                        }
                        else 
                        {
                            lvi.StateImageIndex = ImageIndex.ERROR;
                            this.commandStatus = ImageIndex.ERROR;
                        }
                        break;
                    default:
                        lvi.StateImageIndex = ImageIndex.ERROR;
                        this.commandStatus = ImageIndex.ERROR;
                        break;
                }
            }

            lvi.SubItems.Add(result.GetMessage());
            lvi.SubItems.Add(result.ReturnMsg);
            lvi.SubItems.Add(result.LogFile);

            lsvResult.Items.Add(lvi);
        }

        private void btnStep3_Click(object sender, EventArgs e)
        {
            MessageBox.Show(ConfigurationManager.AppSettings["DB2|0"].ToString());
        }
    }
}
